<?php


namespace app\parent\service;

use app\common\service\BaseService;
use app\lib\exception\BaseException;
use app\models\GradesDetailsListsModel;
use app\models\GradesDetailsModel;
use app\models\ParentInformationModel;
use app\models\StudentModel;
use app\models\StudentSubjectInfosClassModel;

class ClassDayService extends BaseService
{
    /**
     * User: wufeng
     * Date: 2020/03/11.
     *
     * @param $params
     *
     * @return array
     */
    public function classDayDetailLists($params)
    {
        try {
            $where = [];
            $student_id = (new ParentInformationModel())->get(parent_get_current_user_id())['student_id'];
            $campus_id = (new StudentModel())->get($student_id)['campus_id'];
            $where['campus_id'] = $campus_id;
            $where['student_id'] = $student_id;
            $where['class_date'] = $params['class_date'];
            $this->data = (new StudentSubjectInfosClassModel())
                ->where($where)
                ->select()
                ->toArray();
            foreach ($this->data as &$datum) {
				$ids = (new GradesDetailsListsModel())->order('class_date asc')->column('id');
				$i = array_search($datum['class_manage_details_id'], $ids, true);
				$j = $i + 1;
				$datum['class_day'] = '第'.$j.'节';
                $gradesDetails = (new GradesDetailsModel())->get($datum['class_subject_id']);
                $datum['grades_name'] = $gradesDetails['grades_name'];
                $datum['headmaster'] = $gradesDetails['headmaster'];
                $datum['teacher'] = $gradesDetails['teacher'];
                $datum['assistant'] = $gradesDetails['assistant'];
                $gradesDetailsLists = (new GradesDetailsListsModel())->get($datum['class_manage_details_id']);
                $datum['class_date'] = $gradesDetailsLists['class_date'];
                $datum['class_subject_start'] = $gradesDetailsLists['class_subject_start'];
                $datum['class_subject_end'] = $gradesDetailsLists['class_subject_end'];
                $datum['class_subject_end'] = $gradesDetailsLists['class_subject_end'];
            }
            if (empty($this->data)) {
                throw new BaseException('NO_DATA');
            }
        } catch (\Exception $e) {
            $this->errorMessage($e, $params);
        }

        return $this->return;
    }
}
